package com.hnevc.trafficdemotest1.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.hnevc.trafficdemotest1.R;
import com.hnevc.trafficdemotest1.utils.SpUtils;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginActivity extends AppCompatActivity {
    String urls = "http://192.168.1.142:8080/RestaurantServer/action/user_login.do";
    private static final String TAG = "LoginActivity";
    RequestQueue requestQueue;
    String userName;//用户名
    String password;//密码
    boolean rememberPassword;//记录记住密码
    boolean autoLogin;//记录是否自动登录
    CheckBox mCbRememberPassword;
    CheckBox mCbAutoLogin;
    private TextView mTvUserName;
    private TextView mTvPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initData();
        initView();
    }

    //初始化数据
    private void initData() {
        SpUtils sp = new SpUtils(getApplicationContext());
        rememberPassword = sp.getIsRememberPassword();
        autoLogin = sp.getIsAutoLogin();
        userName = sp.getUserName();
        password = sp.getPassword();
    }

    private void initView() {
        mCbRememberPassword = (CheckBox) findViewById(R.id.id_cb_remember_password);
        mCbAutoLogin = (CheckBox) findViewById(R.id.id_cb_auto_login);
        mTvUserName = (TextView) findViewById(R.id.id_et_username);
        mTvPassword = (TextView) findViewById(R.id.id_et_password);

        mCbRememberPassword.setChecked(rememberPassword);
        mCbAutoLogin.setChecked(autoLogin);

        if(userName!=null){
            mTvUserName.setText(userName);
        }
        if(password!=null){
            mTvPassword.setText(password);
        }
    }

    public void doLogin(View view) {


        JSONObject object = new JSONObject();
        try {
            object.put("userName", mTvUserName.getText().toString());
            object.put("userPwd", mTvPassword.getText().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }


        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, urls,object,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        String result = jsonObject.optString("RESULT");

                        if(result.equals("S")){
                            String userRole = jsonObject.optString("UserRole");
                            Toast.makeText(LoginActivity.this, jsonObject
                                    .optString("ERRMSG")+",角色为:"+userRole, Toast.LENGTH_SHORT)
                                    .show();
                            //保存角色到Sp中
                            SpUtils spUtils = new SpUtils(getApplicationContext());
                            spUtils.setRole(userRole);

                            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                            startActivity(intent);
                            finish();
                        }else if(result.equals("F")){
                            Toast.makeText(LoginActivity.this, jsonObject.optString("ERRMSG"), Toast.LENGTH_SHORT).show();
                        }
                        else{
                            Toast.makeText(LoginActivity.this, "登录错误", Toast.LENGTH_SHORT).show();
                        }

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e(TAG, "onErrorResponse: "+volleyError.toString() );
                        Toast.makeText(LoginActivity.this, "登录错误", Toast.LENGTH_SHORT).show();
                    }
                });
        requestQueue = Volley.newRequestQueue(this);

        requestQueue.add(jsonObjectRequest);
    }
}
